package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.QuestionService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionGroupRepository questionGroupRepository;

    @Autowired
    private QuestionSectionRepository questionSectionRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private QuestionPaperRepository questionPaperRepository;


    @Override
    public Page<Question> queryQuestion(Long groupId, Pageable pageable) {
        QQuestion qQuestion = QQuestion.question;
        BooleanBuilder builder = new BooleanBuilder();
        if (null != groupId) {
            builder.and(qQuestion.qGroup.id.eq(groupId));
        }
//        if (!StringUtils.isEmpty(code)) {
//            builder.and(qQuestion.serialNumber.like("%"+code+"%"));
//        }
//        if (!StringUtils.isEmpty(name)) {
//            builder.and(qQuestion.realName.like("%"+name+"%"));
//        }
//        if (!StringUtils.isEmpty(telephone)) {
//            builder.and(qQuestion.telephone.like("%"+telephone+"%"));
//        }
//        if (null!=departmentIds&&departmentIds.length>0) {
//            builder.and(qQuestion.departments.any().id.in(departmentIds));
//        }
        Page<Question> questions = questionRepository.findAll(builder, pageable);
        return questions;
    }

    @Override
    public Question createTextQuestion(
            @ApiParam("是否必填") Long sectionId,
            @ApiParam("分组id") Long groupId,
            @ApiParam("是否必填") boolean require,
            @ApiParam("标题") String title,
            @ApiParam("备注信息") String desc) {
        QuestionGroup questionGroup = null;
        QuestionSection questionSection = null;
        if (null != groupId) {
            questionGroup = questionGroupRepository.findOne(groupId);
        }
        if (null != sectionId) {
            questionSection = questionSectionRepository.findOne(sectionId);
        }
        Question question = new Question();
        question.setCategory(QuestionCategory.TEXT);
        question.setqGroup(questionGroup);
        question.setSection(questionSection);
        question.setRequired(require);
        question.setSubject(title);
        question.setDescription(desc);
        return questionRepository.save(question);
    }

    @Override
    public Question createTextPhotoQuestion(Long sectionId, Long groupId, boolean require, String title, String desc) {
        QuestionGroup questionGroup = null;
        QuestionSection questionSection = null;
        if (null != groupId) {
            questionGroup = questionGroupRepository.findOne(groupId);
        }
        if (null != sectionId) {
            questionSection = questionSectionRepository.findOne(sectionId);
        }
        Question question = new Question();
        question.setCategory(QuestionCategory.UPLOAD_PHOTO);
        question.setqGroup(questionGroup);
        question.setSection(questionSection);
        question.setRequired(require);
        question.setSubject(title);
        question.setDescription(desc);
        return questionRepository.save(question);
    }

    @Override
    public Question createChooseQuestion(Long sectionId, Long groupId, boolean multiChoose, String standardAnswer, boolean require, String title, String desc) {
        QuestionGroup questionGroup = null;
        QuestionSection questionSection = null;
        if (null != groupId) {
            questionGroup = questionGroupRepository.findOne(groupId);
        }
        if (null != sectionId) {
            questionSection = questionSectionRepository.findOne(sectionId);
        }
        Question question = new Question();
        if (multiChoose) {
            question.setCategory(QuestionCategory.CHECKBOX);
        } else {
            question.setCategory(QuestionCategory.RADIO);
        }
        question.setqGroup(questionGroup);
        question.setSection(questionSection);
        question.setRequired(require);
        question.setSubject(title);
        question.setStandardAnswer(standardAnswer);
        question.setDescription(desc);
        return questionRepository.save(question);
    }


    @Override
    public List<QuestionSection> getQuestionSections(QuestionPaper.QuestionPaperCategory category) {
        QuestionPaper questionPaper = getPaper(category);
        QQuestionSection qQuestionSection = QQuestionSection.questionSection;
        BooleanBuilder builder = new BooleanBuilder();
        if (null != questionPaper) {
            builder.and(qQuestionSection.deleted.eq(false));
            builder.and(qQuestionSection.paper.id.eq(questionPaper.getId()));
            Page<QuestionSection> page = questionSectionRepository.findAll(builder, new PageRequest(0, 1000));
            return page.getContent();
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<QuestionGroup> getQuestions(Long sectionId) {
        QQuestionGroup qQuestionGroup = QQuestionGroup.questionGroup;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionGroup.deleted.eq(false));
        builder.and(qQuestionGroup.questionSection.id.eq(sectionId));
        Page<QuestionGroup> questionGroups = questionGroupRepository.findAll(builder,
                new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "creationTime")));
        return questionGroups.getContent();
    }

    @Override
    public List<Question> getQuestionsByGroup(Long groupId) {
        QQuestion qQuestion = QQuestion.question;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestion.deleted.eq(false));
        builder.and(qQuestion.enabled.eq(true));
        builder.and(qQuestion.qGroup.id.eq(groupId));
        Page<Question> questions = questionRepository.findAll(builder,
                new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));

        return questions.getContent();
    }

    @Override
    public List<QuestionAnswerOption> getOptionsByGroup(Long questionId) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        Page<QuestionAnswerOption> questions = questionAnswerOptionRepository.findAll(builder, new PageRequest(0, 1000,new Sort(Sort.Direction.ASC, "idx")));
        return questions.getContent();
    }

    private QuestionPaper getPaper(QuestionPaper.QuestionPaperCategory category) {
        QQuestionPaper qQuestionPaper = QQuestionPaper.questionPaper;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionPaper.deleted.eq(false));
        builder.and(qQuestionPaper.category.eq(category));
        Page<QuestionPaper> questionPapers = questionPaperRepository.findAll(builder, new PageRequest(0, 100));
        QuestionPaper questionPaper = null;
        for (QuestionPaper savedQuestionPaper : questionPapers) {
            questionPaper = savedQuestionPaper;
        }
        return questionPaper;
    }


}
